﻿<%@ Page Language="VB" MasterPageFile="~/Default.master" Title="Untitled Page" %>

<%@ Register Assembly="AjaxControlToolkit" Namespace="AjaxControlToolkit" TagPrefix="cc1" %>

<%@ Register Assembly="System.Web.Extensions, Version=1.0.61025.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35"
    Namespace="System.Web.UI" TagPrefix="asp" %>

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.Sql" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="ClubStarterKit.Data" %>
<%@ Import Namespace="SubSonic" %>

<%@ Register TagPrefix="Club" TagName="LoginBanner" Src="~/UserControls/LoginBanner.ascx" %>
<%@ Register TagPrefix="Club" TagName="ImageThumbnail" Src="~/UserControls/ImageThumbnail.ascx" %>

<script runat="server">

    Public MaxAddToRSVP As Integer
   
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
        If Request("eventid") Is Nothing Then
            Response.Redirect("~/events/list.aspx")
        End If

        If IsPostBack = False Then
            Dim NextID As String = ClubStarterKit.Web.Events.NextEvent(EventID)
            Dim PrevID As String = ClubStarterKit.Web.Events.PrevEvent(EventID)
            
            InitRSVP()
            
            If Not NextID = "-" Then
                NextButton.NavigateUrl = "~/events/view.aspx?eventid=" & NextID
            Else
                NextButton.Enabled = False
            End If
            
            If Not PrevID = "-" Then
                PrevButton.NavigateUrl = "~/events/view.aspx?eventid=" & PrevID
            Else
                PrevButton.Enabled = False
            End If
            
            panel1.Visible = User.IsInRole("Administrators")
            
            ClubStarterKit.Web.Events.RSVPSanityCheck(EventID)
        End If
    End Sub
    
    Protected Sub InitRSVP()
        Dim TheEvent As New ClubEvent(EventID)
        If TheEvent.IsLoaded = True Then
            If TheEvent.RSVP = True Then
                If User.Identity.IsAuthenticated = True Then
                    StatusLabel.Text = "This event requires an RSVP."
                    ShowRSVP()
                Else
                    StatusLabel.Text = "This event requires an RSVP. Please login to RSVP."
                End If
            Else
                StatusLabel.Text = "This event doesn't require an RSVP."
            End If
        Else
            StatusLabel.Text = ""
        End If
    End Sub
    
    Protected Sub ShowRSVP()
        Dim TheEvent As New ClubEvent(EventID)
        RSVPDiv.Visible = True
        Dim current As Integer = Integer.Parse(TheEvent.CurrentAttendance.ToString())
        Dim total As Integer = Integer.Parse(TheEvent.TotalAttendance.ToString())
        If current = total Then
            rsvptoevent.Visible = False
            RSVPTTLStatus.Text = "The number of attendees to this event is <strong>" & total.ToString() & "</strong>. That compacity has been filled."
        Else
            Dim qry As New Query(Tables.Attendance)
            Dim mem As MembershipUser = Membership.GetUser
            Dim gui As String = mem.ProviderUserKey.ToString
            qry.AddWhere(Attendance.Columns.Member, gui)
            qry.AddWhere(Attendance.Columns.Eventid, EventID)
            Dim rc As Integer = qry.GetRecordCount()
            If rc = 0 Then
                rsvptoevent.Visible = True
                delrsvp.Visible = False
            Else
                delrsvp.Visible = True
                rsvptoevent.Visible = False
            End If
            MaxAddToRSVP = total - current
            RSVPTTLStatus.Text = "There are <strong>" & MaxAddToRSVP.ToString() & "</strong> more attendees permitted to attend."
        End If
    End Sub
    
    Function ShowLocationLink(ByVal locationname As Object, ByVal locationdirections As Object) As String
        If (Not locationname Is Nothing) AndAlso Not IsDBNull(locationname) Then
            Return "At " & CStr(locationname) & "<br/>"
        Else
            Return ""
        End If
    End Function
    
    Function StartDT(ByVal starttime As Object) As String
        Dim starttimeDT As Date = CDate(starttime)
        If Not starttime Is Nothing AndAlso Not IsDBNull(starttime) Then
            Return starttimeDT.ToLongDateString() & " at " & starttimeDT.ToShortTimeString()
        Else
            Return " "
        End If
    End Function
    
    Function EndDT(ByVal endtime As Object) As String
        Dim endtimeDT As Date = CDate(endtime)
        If Not endtime Is Nothing AndAlso Not IsDBNull(endtime) Then
            Return "to " & endtimeDT.ToLongDateString() & " at " & endtimeDT.ToShortTimeString()
        Else
            Return " "
        End If
    End Function
    
    Property EventID() As Integer
        Get
            Dim m_EventID As Integer
            Dim id As Object = ViewState("EventID")
            If Not id Is Nothing AndAlso IsNumeric(id) Then
                m_EventID = CInt(id)
            Else
                id = Request.QueryString("EventID")
                If Not id Is Nothing AndAlso IsNumeric(id) Then
                    m_EventID = CInt(id)
                Else
                    m_EventID = 1
                End If
                
                ViewState("EventID") = m_EventID
            End If
            Return m_EventID
        End Get
        Set(ByVal value As Integer)
            ViewState("EventID") = value
        End Set
    End Property

    Protected Sub SendRSVP_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        If Not NumGuests.Text = "" AndAlso IsDBNull(NumGuests.Text) = False Then
            Dim mem As MembershipUser = Membership.GetUser
            Dim gui As String = mem.ProviderUserKey.ToString
            Dim num As Integer = Integer.Parse(NumGuests.Text)
                        
            Dim theevent As New ClubEvent(EventID)
            Dim current As Integer = Integer.Parse(CStr(theevent.CurrentAttendance))
            If num >= MaxAddToRSVP Then
                SPs.AddAttendance(CType(mem.ProviderUserKey, Guid), num, EventID).Execute()
                Dim qry As New Query(Tables.ClubEvent)
                qry.QueryType = QueryType.Update
                qry.AddWhere(ClubEvent.Columns.ID, EventID)
                qry.AddUpdateSetting(ClubEvent.Columns.CurrentAttendance, current + num)
                qry.Execute()
                Response.Redirect("~/events/view.aspx?eventid=" & EventID)
            Else
                ErrorLbl.Visible = True
                ErrorLbl.Text = "The number of guests you entered exceeds the limit."
            End If
        Else
            ErrorLbl.Visible = True
            ErrorLbl.Text = "Please Enter the number of guests."
        End If
        
    End Sub

    Protected Sub DelRSVPButton_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim mem As MembershipUser = Membership.GetUser
        Dim gui As String = mem.ProviderUserKey.ToString
        
        Dim delqry As New Query(Tables.Attendance)
        delqry.QueryType = QueryType.Delete
        delqry.AddWhere(Attendance.Columns.Eventid, EventID)
        delqry.AddWhere(Attendance.Columns.Member, gui)
        delqry.Execute()
        
        ClubStarterKit.Web.Events.RSVPSanityCheck(EventID)
        
        Response.Redirect("~/events/view.aspx?eventid=" & EventID)
    End Sub

    Protected Sub SendEmail_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Response.Redirect("~/Admin/EventEmail.aspx?id=" & CStr(Request("eventid")))
    End Sub

    Protected Sub EditBtn_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Response.Redirect("Edit.aspx?Action=Edit&id=" & CStr(Request("eventid")))
    End Sub

    Protected Sub HyperLink2_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Response.Redirect("~/Events/Download.aspx?eventid=" & CStr(Request("eventid")))
    End Sub
</script>

<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<div id="body">
        <asp:ScriptManager id="sm" runat="server">
        </asp:ScriptManager>
        <Club:LoginBanner ID="LoginBanner1" runat="server" />
        <div id="columnleft">
            <div class="leftblock">
            <h2>RSVP</h2>
            <br />
            <asp:Label ID="StatusLabel" runat="server" />
                <asp:ObjectDataSource ID="RSVPSource" runat="server" SelectMethod="OpenRSVP" TypeName="ClubStarterKit.Web.Events">
                    <SelectParameters>
                        <asp:QueryStringParameter Name="EventID" QueryStringField="eventid" Type="Int32" />
                    </SelectParameters>
                </asp:ObjectDataSource>
                <br />
                <div id="RSVPDiv" runat="server" visible="false">
                    <div id="delrsvp" runat="server" visible="false">
                    You have already sent an RSVP.<br />
                    <asp:Button ID="DelRSVPButton" runat="server" Text="Un-RSVP" OnClick="DelRSVPButton_Click" />
                    </div>
                    <div id="rsvptoevent" runat="server" visible="false">
                    <br /><br /><h5>RSVP to Event</h5>
                    Number of Guests: <asp:TextBox ID="NumGuests" runat="server" Width="50px"></asp:TextBox>
                    
                    <asp:Button ID="SendRSVP" runat="server" Text="RSVP" OnClick="SendRSVP_Click" /><br />
                    <asp:Label ID="ErrorLbl" runat="server" Visible="false" ForeColor="red" />
                    </div>
                    <br />
                   <br />
                   <asp:Label ID="RSVPTTLStatus" runat="server" />
                   <asp:GridView id="RSVPGrid" runat="server" DataSourceID="RSVPSource" Width="208px" BorderWidth="1px" BorderStyle="None" BackColor="White" BorderColor="#CCCCCC" CellPadding="3" DataMember="DefaultView" AutoGenerateColumns="False" Visible='<%# User.IsInRole("Administrators") %>'>
                    <FooterStyle BackColor="White" ForeColor="#000066" />
                    <RowStyle ForeColor="#000066" HorizontalAlign="Center" />
                    <SelectedRowStyle BackColor="#669999" Font-Bold="True" ForeColor="White" />
                    <HeaderStyle BackColor="#006699" Font-Bold="True" ForeColor="White" />
                    <Columns>
                        <asp:TemplateField SortExpression="avatar">
                            <ItemTemplate>
                                <asp:Image ID="Image1" runat="server" ImageUrl='<%# "~/UserControls/AvatarImagefetch.ashx?MemberId=" & DataBinder.Eval(Container.DataItem,"UserId").ToString %>' ToolTip='<%# Eval("UserName") %>' />
                            </ItemTemplate>
                            <ItemStyle Width="104px" />
                        </asp:TemplateField>
                        <asp:TemplateField SortExpression="guests">
                            <ItemTemplate>
                                <asp:Label ID="lblguests" runat="server" Text='<%# Eval("numofguest") %>' />
                            </ItemTemplate>
                            <ItemStyle Width="104px" />
                        </asp:TemplateField>
                    </Columns>
                    <EmptyDataTemplate>
                    No one has sent an RSVP for this event.
                    </EmptyDataTemplate>
                </asp:GridView>
                </div>
                
            </div>
        </div>
                <div id="columnright">
            <div class="rightblock">
                <a href="calendar.aspx">month view</a> <a href="list.aspx">list view</a></div>
                <div class="rightblock">
                <asp:FormView ID="FormView" runat="server" DataSourceID="ObjectDataSource1">
                    <ItemTemplate>
<h2>
                            <asp:Label Text='<%# Eval("title") %>' runat="server" ID="titleLabel" />
                        </h2>
                        <div class="itemdetails">
                            <br />
                            <h3>
                                <asp:Label ID="locationLabel" runat="server" Text='<%# ShowLocationLink(Eval("LocationName"),Eval("LocationDirections")) %>' />
                            </h3>
                            <p>
                                <asp:Label Text='<%# StartDT(Eval("starttime")) %>' runat="server" ID="itemdateLabel" />
                                <br />
                                <asp:Label Text='<%# EndDT(Eval("endtime")) %>' runat="server"
                                    ID="Label1" />
                            </p>
                            <p>
                                <asp:Label Font-Size="Smaller" Text='<%# Eval("description") %>' runat="server" ID="descriptionLabel" />&nbsp;</p>
                        </div>
                        <br />
                        <div class="downloadevent">
                            <a href='<%# "download.ashx?EventID=" & CStr(Eval("id")) %>'>
                                <asp:Image ID="Image3" SkinID="download" runat="server"  />
                                </a><a
                                    href='<%# "download.ashx?EventID=" & CStr(Eval("id")) %>'>Add this event to your
                                    personal calendar</a><br />
                            <a target="_blank" href='<%# "print.aspx?EventID=" & CStr(Eval("id")) %>'>
                                <asp:Image SkinID="print"  ID="Image2" runat="server" />
                                </a><a target="_blank"
                                    href='<%# "Print.aspx?EventID=" & CStr(Eval("id")) %>'>Printer Friendly Version</a>
                        </div>
                        <Club:ImageThumbnail ID="thumb1" runat="server" ImageSize="Large" PhotoID='<%# Eval("photo") %>' />                    </ItemTemplate>
                </asp:FormView>
                <br />
                    <div class="nextlink">
                        <asp:HyperLink ID="NextButton" runat="server" Text="Next >"></asp:HyperLink>
                    </div>
                        <asp:HyperLink ID="PrevButton" runat="server" Text="< Previous"></asp:HyperLink>
                    
                    <div id="panel1" runat="server" class="actionbuttons">
                            <Club:RolloverButton ID="SendEmail" CausesValidation="false" runat="server" Text="Send Email" OnClick="SendEmail_Click" />
                            <Club:RolloverButton ID="EditBtn" CausesValidation="false" runat="server" Text="Edit" OnClick="EditBtn_Click" />
                            <Club:RolloverButton ID="HyperLink2" CausesValidation="false" runat="server" Text="Downloads" OnClick="HyperLink2_Click" />
                        </div>
                    
                    <asp:ObjectDataSource ID="ObjectDataSource1" runat="server" SelectMethod="FetchByParameter"
                        TypeName="ClubStarterKit.Data.ClubEvent">
                        <SelectParameters>
                            <asp:Parameter DefaultValue="ID" Name="columnName" Type="String" />
                            <asp:Parameter DefaultValue="" Name="comparison" Type="Object" />
                            <asp:QueryStringParameter DefaultValue="" Name="oValue" QueryStringField="EventID"
                                Type="Object" />
                        </SelectParameters>
                    </asp:ObjectDataSource>
                    <br />
                </div>
        </div>
        </div>
</asp:Content>